﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using BLL;

public partial class Admin_Department_DepartmentMove : System.Web.UI.Page
{
    DepartmentInfoManager dllDepartmentInfoManager = new DepartmentInfoManager();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            bindTreeView();
        }
    }


    /// <summary>
    /// 绑定TreeView,同时标明状态
    /// </summary>
    /// <author>grokyao</author>
    /// <date>2009-1-15</date>
    void bindTreeView()
    {
        DataTable _department = dllDepartmentInfoManager.get();
        string _companyName = System.Configuration.ConfigurationManager.AppSettings["CompanyName"].ToString();
        TreeNode _root = new TreeNode(_companyName);
        this.trvDepartment.Nodes.Add(_root);
        for (int i = 0; i < _department.Rows.Count; i++)
        {
            TreeNode _departmentNode;
            if (_department.Rows[i]["DepartmentState"].ToString() == "1")
            {
                _departmentNode = new TreeNode(_department.Rows[i]["DepartmentName"].ToString() + "(停用状态)");
                _departmentNode.ImageUrl = "~/images/Tree_department.gif";
            }
            else
            {
                _departmentNode = new TreeNode(_department.Rows[i]["DepartmentName"].ToString());
                _departmentNode.ImageUrl = "~/images/Tree_department.gif";
            }
            _departmentNode.Value = _department.Rows[i]["DepartmentId"].ToString();
            _root.ChildNodes.Add(_departmentNode);
            RecursionTreeNode(_departmentNode);
        }
    }




    /// <summary>
    /// 递归方法
    /// </summary>
    void RecursionTreeNode(TreeNode _parent)
    {
        object[] _value = new object[1];
        _value[0] = Convert.ToInt32(_parent.Value);
        DataTable _department = dllDepartmentInfoManager.get(DepartmentInfoManager.field.DepartmentParentId.ToString(), _value);
        for (int i = 0; i < _department.Rows.Count; i++)
        {
            TreeNode _child = new TreeNode();
            _child.ImageUrl = "~/images/Tree_department.gif";
            _child.Text = _department.Rows[i]["DepartmentName"].ToString();
            _child.Value = _department.Rows[i]["DepartmentId"].ToString();
            _parent.ChildNodes.Add(_child);
            RecursionTreeNode(_child);
        }
    }


    /// <summary>
    /// TreeView节点选择事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void trvDepartment_SelectedNodeChanged(object sender, EventArgs e)
    {
        //获取选择的TreeView项
        //TreeNode tn = this.trvDepartment.SelectedNode;
        //string _companyName = System.Configuration.ConfigurationManager.AppSettings["CompanyName"].ToString();
        //if (tn != null && tn.Text != _companyName)
        //{
        //    object[] _value = new object[2];
        //    _value[0] = Utility.HttpHelp.getParamsInt("DepartmentId");
        //    _value[1] = Convert.ToInt32(tn.Value);
        //    根据部门id修改部门父级
        //    int result = dllDepartmentInfoManager.edit(DepartmentInfoManager.field.DepartmentParentId.ToString(), _value);
        //    if (result == 0)
        //    {
        //        Utility.Error.RedirectErrorPage("移动部门未成功，请重试！");
        //    }
        //    else
        //    {
        //        Utility.HttpHelp.closeWindow("Department_Manager.aspx",Page);
        //    }
        //}
        //else
        //{
        //    Utility.HttpHelp.alert("请选择部门节点", Page);
        //}
    }


    /// <summary>
    /// 确定事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    /// <author>grokyao</author>
    /// <date>2009-2-13</date>
    protected void btnOk_Click(object sender, EventArgs e)
    {
        TreeNode tn = this.trvDepartment.SelectedNode;
        string _companyName = System.Configuration.ConfigurationManager.AppSettings["CompanyName"].ToString();
        if (tn != null)
        {
            object[] _value = new object[2];
            _value[0] = Utility.HttpHelp.getParamsInt("DepartmentId");
            if (tn.Text == _companyName)
            {
                _value[1] = 0;
            }
            else
            {
                _value[1] = Convert.ToInt32(tn.Value);
            }            
            //根据部门id修改部门父级
            int result = dllDepartmentInfoManager.edit(DepartmentInfoManager.field.DepartmentParentId.ToString(), _value);
            if (result == 0)
            {
                Utility.Error.RedirectErrorPage("移动部门未成功，请重试！");
            }
            else
            {
                Utility.HttpHelp.closeWindow("Department_Manager.aspx", Page);
            }
        }
        else
        {
            Utility.HttpHelp.alert("请选择部门节点", Page);
        }
    }
}
